package com.open.rbac.service.sys.impl;

import java.util.List;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.open.center.common.util.CopyUtils;
import com.open.rbac.model.dto.sys.RoleMenuRelDto;
import com.open.rbac.model.entity.sys.RoleMenuRel;
import com.open.rbac.model.vo.sys.RoleMenuRelVo;
import com.open.rbac.service.sys.IRoleMenuRelService;
import com.open.rbac.service.sys.repository.RoleMenuRelRepository;

/**
 * 角色与菜单关系服务
 *
 * @author Riche's
 * @since 2025/9/3
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RoleMenuRelServiceImpl implements IRoleMenuRelService {

    private final RoleMenuRelRepository roleMenuRelRepository;

    @Transactional
    @Override
    public void create(List<RoleMenuRelVo> relVos) {
        List<RoleMenuRel> rels = relVos.stream().map(relVo -> {
            RoleMenuRel rel = new RoleMenuRel();
            rel.setRoleId(relVo.getRoleId());
            rel.setMenuId(relVo.getMenuId());
            return rel;
        }).toList();
        roleMenuRelRepository.saveAll(rels);
    }

    @Transactional
    @Override
    public void deleteReallyByRoleId(String roleId) {
        roleMenuRelRepository.deleteReallyByRoleId(roleId);
    }

    @Transactional
    @Override
    public void deleteReallyByMenuId(String menuId) {
        roleMenuRelRepository.deleteReallyByMenuId(menuId);
    }

    @Override
    public List<RoleMenuRelDto> listByUserId(String userId) {
        return CopyUtils.listCopy(roleMenuRelRepository.findByUserId(userId), RoleMenuRelDto.class);
    }
}
